package com.zhongxiangspace.controller.admin;

import com.zhongxiangspace.entity.*;
import com.zhongxiangspace.result.ExceptionResult;
import com.zhongxiangspace.result.NormalResult;
import com.zhongxiangspace.result.Result;
import com.zhongxiangspace.service.CountyService;
import com.zhongxiangspace.service.OfficeBuildingService;
import com.zhongxiangspace.service.StationBranchService;
import com.zhongxiangspace.tools.FreeMarkerViewUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class StationDetailMobileHtmlCreateController {
    @Autowired
    private StationBranchService stationBranchService;
    @Autowired
    private CountyService countyService;
    @Autowired
    private OfficeBuildingService officeBuildingService;

    @GetMapping("/htmlCreate/html/mobileStationdetails.do")
    public boolean stationDetailMobileHtml( Integer id) {
       try {
        FreemarkerHtmlConfiguration freemarkerHtmlConfiguration = new FreemarkerHtmlConfiguration();
        freemarkerHtmlConfiguration.setTemplateName("mobile_station.ftl");
        freemarkerHtmlConfiguration.setHtmlPath("/html/wap/details/s"+id+".html");
        freemarkerHtmlConfiguration.setTemplatePath("html/mobile");
        Map<String, Object> map = new HashMap<>();
        //工位数据
        StationBranch stationBranch = this.stationBranchService.queryOne(id);
        map.put("stationBranch", stationBranch);
        County county = this.countyService.queryOne(stationBranch.getCounty_id());
        map.put("county", county);
        County trade_area = this.countyService.queryOne(stationBranch.getTrade_area_id());
        map.put("trade_area", trade_area);
        OfficeBuildings office = this.officeBuildingService.queryOne(stationBranch.getBuilding_id());
        map.put("office", office);
        BuildingsAttribute buildingsAttribute = this.officeBuildingService.queryAttr(office.getBuilding_id());
        map.put("buildingsAttribute", buildingsAttribute);
        List<StationHouse> stationHouse = this.stationBranchService.queryStationHouse(id);
        map.put("stationHouse", stationHouse);
        List<StationIcon> stationIconList = this.stationBranchService.queryIcon(null);
        map.put("stationIconList", stationIconList);
        //热门楼盘
        QueryAnalyzer buildingQueryAnalyzer = new QueryAnalyzer();
        buildingQueryAnalyzer.setShowCode(1);
        buildingQueryAnalyzer.setHotCode(1);
        List<Map<String, Object>> officeBuildingsList = this.officeBuildingService.queryOfficeBuilding(buildingQueryAnalyzer);
        map.put("officeBuildings", officeBuildingsList);
        freemarkerHtmlConfiguration.setData(map);
         boolean falg= FreeMarkerViewUtil.createHtml(freemarkerHtmlConfiguration);
         return  falg;
       }catch (Exception e){
            e.printStackTrace();
            return  false;
       }
    }


    /**
     * 工位详情页
     *
     * @param request
     * @param pageType
     * @return
     */
    @ResponseBody
    @PostMapping("/htmlCreate/html/mobileStationdetails.do")
    public Result ajxaCreateStation(HttpServletRequest request, String pageType) {
        try {
            QueryAnalyzer queryAnalyzer = new QueryAnalyzer();
            List<Map<String, Object>> stationBranchList = this.stationBranchService.queryAll(queryAnalyzer);
            for (int i = 0; i < stationBranchList.size(); i++) {
                stationDetailMobileHtml((Integer) stationBranchList.get(i).get("station_id"));
            }
            return new NormalResult(true);
        } catch (Exception e) {
            return new ExceptionResult(e);
        }
    }


}
